package com.fh.controller.app.sysuser;

import com.fh.controller.base.BaseController;
import com.fh.service.dog.LeaguerService;
import com.fh.service.system.fhlog.FHlogManager;
import com.fh.service.system.user.UserManager;
import com.fh.util.*;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.session.Session;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;


/**@author FH Q313596790
  * 系统用户-接口类 
  * 相关参数协议：
  * 00	请求失败
  * 01	请求成功
  * 02	返回空值
  * 03	请求协议参数不完整    
  * 04  用户名或密码错误
  * 05  FKEY验证失败
 */
@Controller
@RequestMapping(value="/appSysUser")
public class SysUserController extends BaseController {
    
	@Resource(name="userService")
	private UserManager userService;
	@Resource(name="fhlogService")
	private FHlogManager FHLOG;
	@Resource(name="leaguerService")
	private LeaguerService leaguerService;
	
	/**系统用户注册接口
	 * @return
	 */
	@RequestMapping(value="/registerSysUser")
	@ResponseBody
	public Object registerSysUser(){
		logBefore(logger, "系统用户注册接口");
		Map<String,Object> map = new HashMap<String,Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try{
			if(Tools.checkKey("USERNAME", pd.getString("FKEY"))){	//检验请求key值是否合法
				if(AppUtil.checkParam("registerSysUser", pd)){		//检查参数
					
					Session session = Jurisdiction.getSession();
					String sessionCode = (String)session.getAttribute(Const.SESSION_SECURITY_CODE);		//获取session中的验证码
					String rcode = pd.getString("rcode");
					if(Tools.notEmpty(sessionCode) && sessionCode.equalsIgnoreCase(rcode)){				//判断登录验证码
						pd.put("USER_ID", this.get32UUID());	//ID 主键
						pd.put("ROLE_ID", "fhadminzhuche");	//角色ID fhadminzhuche 为注册用户
						pd.put("NUMBER", "");					//编号
						pd.put("PHONE", "");					//手机号
						pd.put("BZ", "注册用户");				//备注
						pd.put("LAST_LOGIN", "");				//最后登录时间
						pd.put("IP", "");						//IP
						pd.put("STATUS", "0");					//状态
						pd.put("SKIN", "default");
						pd.put("RIGHTS", "");		
						pd.put("PASSWORD", new SimpleHash("SHA-1", pd.getString("USERNAME"), pd.getString("PASSWORD")).toString());	//密码加密
						if(null == userService.findByUsername(pd)){	//判断用户名是否存在
							userService.saveU(pd); 					//执行保存
							FHLOG.save(pd.getString("USERNAME"), "新注册");
						}else{
							result = "04"; 	//用户名已存在
						}
					}else{
						result = "06"; 		//验证码错误
					}
				}else {
					result = "03";
				}
			}else{
				result = "05";
			}
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}

	/**
	 * 保存新用户 dog 保存用户
	 *
	 * @param
	 * @return PageData
	 * @throws Exception
	 */
	@RequestMapping(value = "/save")
	@ResponseBody
	public PageData save() throws Exception {
		PageData result = new PageData();
		PageData pd = this.getPageData();
		//ID 主键
		pd.put("user_id", this.get32UUID());
		//角色
		pd.put("ROLE_ID", "2");
		//最后登录时间
		pd.put("LAST_LOGIN", "");
		//IP
		pd.put("IP", "");
		//状态
		pd.put("STATUS", "0");
		pd.put("SKIN", "default");
		pd.put("RIGHTS", "");
		//密码加密
		pd.put("password", new SimpleHash("SHA-1", pd.getString("USERNAME"), pd.getString("PASSWORD")).toString());
		try {
			//判断用户名是否存在
			if (null == userService.findByUsername(pd)) {
				//执行保存
//				userService.saveU(pd);
				pd.put("member_class",1);//团体0   个人1
				pd.put("member_type",0);//
				pd.put("validate",1);//
				pd.put("is_open",1);//
				pd.put("user_sex",1);//1男  2 女
				pd.put("id",this.get32UUID());
				pd.put("user_name",pd.getString("NAME"));
				pd.put("login_name",pd.getString("USERNAME"));
				leaguerService.save(pd);
				result.put("data", "");
				result.put("returnCode", "00");
				result.put("returnMsg", "新增用户成功");
			} else {
				result.put("data", "");
				result.put("returnCode", "11");
				result.put("returnMsg", "用户已存在");
			}
		} catch (Exception e) {
			logger.error(e.toString(), e);
			result.put("data", "");
			result.put("returnCode", "01");
			result.put("returnMsg", "新增用户失败");
		}
		return result;
	}
	

	
}
	
 